#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using P = pair<int, int>;

int main() {
  ll n, k;
  cin >> n >> k;
  ll S = n * 2, T = n * 2 + 1;
  ll B = 1e9 + 1;
  mcf_graph<int, ll> graph(n * 2 + 2);
  graph.add_edge(S, T, k * n, B);
  rep(i, n) {
    graph.add_edge(S, i, k, 0);
    graph.add_edge(i + n, T, k, 0);
  }
  rep(i, n) rep(j, n) {
    ll a;
    cin >> a;
    graph.add_edge(i, j + n, 1, B - a);
  }
  auto [max_flow, min_cost] = graph.flow(S, T, n * k);
  vector<string> grid(n, string(n, '.'));
  const auto &edges = graph.edges();
  for (const auto &e : edges) {
    // cout << e.from << " " << e.to << " " << e.flow << endl;
    if (e.from == S || e.to == T || e.flow == 0) {
      continue;
    }
    grid[e.from][e.to - n] = 'X';
  }
  cout << B * n * k - min_cost << endl;
  for (int i = 0; i < n; i++) {
    cout << grid[i] << endl;
  }
}
